package com.zengqingfa.designpattern.create.singleton;

/**
 *
 * @fileName: Singleton3
 * @author: zengqf3
 * @date: 2021-4-5 21:00
 * @description: 静态延迟初始化实现单例（懒汉式）线程安全
 */
public class Singleton3 {

    /**
     * 构造函数私有
     */
    private Singleton3() {
    }

    public static Singleton3 instance;

    //获取实例
    public static synchronized Singleton3 getInstance() {
        if (instance == null) {
            instance = new Singleton3();
        }
        return instance;
    }

    public static void main(String[] args) {
        Singleton3 instance1 = Singleton3.getInstance();
        Singleton3 instance2 = Singleton3.getInstance();
        //com.zengqingfa.designpattern.create.singleton.Singleton3@58372a00
        System.out.println(instance1);
        //ture
        System.out.println(instance2 == instance1);
    }
}
